package com.yf.springboot.springboot.web;

import com.yf.springboot.springboot.bean.Car;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class ParamController {

    /*
     /car/1/owner=yf?age=18&hobby=basketball&hobby=volleyball
     这种传参方式叫做queryString,可以使用@RequestParam来接收

     /car/{sn}中的sn使用@PathVariable来接收

     @PathVariable Map<String, String> pv 可以接收路径中的所有pathVariable
     */
    @GetMapping("/car/{sn}/{name}")
    public Map<String, Object> getCar(@PathVariable("sn") Integer sn,
                                      @PathVariable("name") String name,
                                      @PathVariable Map<String, String> pv,
                                      @RequestParam Integer age,
                                      @RequestParam List<String> equipments,
                                      @RequestHeader("User-Agent") String userAgent,
                                      @RequestHeader Map<String, String> headers
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("sn", sn);
        map.put("name", name);
        map.put("pv", pv);
        map.put("userAgent", userAgent);
        map.put("headers", headers);
        map.put("age", age);
        map.put("equipments", equipments);

        return map;
    }

    @PostMapping("/car/add")
    public Map<String, Object> postCar(@RequestBody Car car) {
        Map<String, Object> map = new HashMap<>();
        map.put("sn", car.getSn());
        map.put("name", car.getName());
        log.info("/car/add: ", car);

        return map;
    }

    /*
     应用场景：cookie被禁用了，session里的内容怎么获取
     url重写：/path;jsessionid=xxx,把cookie的值使用矩阵变量的方式传递。
     如果使用queryString的方式传递jsessionid与普通的传参没有分别，不能区别于普通参数
     矩阵变量传参方式一：/cars/conf;sn=1;equipments=benz;equipments=bmw
     矩阵变量传参方式二：/cars/conf;sn=1;equipments=benz,bmw
     请求返回400
     springboot默认是禁用矩阵变量功能的，需要手动开启：urlPathHelper.setRemoveSemicolonContent(false);
     请求返回404
     分号参数是通过路径变量接收的，矩阵变量是通过解析路径变量中分号之后的部分取值的。
     */
    @GetMapping("/cars/{path}")
    public Map<String, Object> getCarEquipments(
        @PathVariable String path,
        @MatrixVariable("sn") Integer sn,
        @MatrixVariable("equipments") List<String> equipments
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("path", path);
        map.put("equipments", equipments);

        return map;
    }

    @GetMapping("/cars/{car1}/{car2}")
    public Map<String, Object> carsSell(
        @PathVariable("car1") Integer car1Sn,
        @PathVariable("car2") Integer car2Sn,
        @MatrixVariable(value = "age", pathVar = "car1") Integer car1Age,
        @MatrixVariable(value = "age", pathVar = "car2") Integer car2Age
    ) {
        Map<String, Object> map = new HashMap<>();
        Car car1 = new Car();
        car1.setSn(car1Sn);
        car1.setAge(car1Age);
        Car car2 = new Car();
        car2.setSn(car2Sn);
        car2.setAge(car2Age);
        map.put("car1", car1);
        map.put("car2", car2);

        return map;
    }

    /*
    测试多视图的内容视图管理的协商策略
     */
    @GetMapping("/accept")
    public Map<String, Object> accept() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "yvan");
        map.put("age", 38);

        return map;
    }
}
